/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author bimemis
 */
import java.io.IOException;
import bdd.beans.DBLink;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

public class ConnexionServlet extends HttpServlet {

    private final static int ROLEADMIN = 4;

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
											throws IOException, ServletException{

        System.out.println("birkan");
		String login = (String)request.getParameter("login");
		String password = (String)request.getParameter("password");
                System.out.println(login);
                DBLink base = new DBLink();
                base.connexion();
                if(base.estReconnue(login, password))
                {
                   PreparedStatement pst;
                   Connection connection=null;
                   ResultSet rs;
                   int numEnseignant=0;
                   int idRole=ROLEADMIN+1;

                   HttpSession session = request.getSession();
                   session.setAttribute("login", login);
                   session.setAttribute("password", password);

                   DBLink bdd = new DBLink();
                   bdd.connexion();
                   connection=bdd.getConnection();
                        try {
                            pst = connection.prepareStatement("SELECT ENSEIGNANT.NUMENSEIGNANT, JNTROLEENSEIGNANT.IDROLE FROM ( ENSEIGNANT"
                                    + "  JOIN JNTROLEENSEIGNANT ON ENSEIGNANT.NUMENSEIGNANT = JNTROLEENSEIGNANT.NUMENSEIGNANT ) "
                                    + "  JOIN \"ROLE\" ON JNTROLEENSEIGNANT.NUMENSEIGNANT=\"ROLE\".IDROLE WHERE LOGIN='"+login+"'");
                            //pst.setString(1, login);
                            rs = pst.executeQuery();

                            while(rs.next() && idRole!=ROLEADMIN) {
				numEnseignant=rs.getInt("NUMENSEIGNANT");
                                idRole=rs.getInt("IDROLE");

                            }

                        }
                        catch(SQLException se) {
                            System.out.println("Erreur de requête");
                        }
                   session.setAttribute("numEns",numEnseignant);
                   session.setAttribute("isAdmin", idRole==ROLEADMIN ? "true" : "false");
                   request.getRequestDispatcher("index.jsp").forward(request, response);
                } else
                {
                    response.getWriter().print("Erreur d'anthentification");
                }



		//La gestion du cookie
		if(request.getParameter("auto_connect") != null){
			System.out.println("Création des cookies.");
			Cookie loginCookie = new Cookie("login", login);
			Cookie passwordCookie = new Cookie("password", password);
			response.addCookie(loginCookie);
			response.addCookie(passwordCookie);
		}

	}
}
